package com.example.fengyungo.utils;

import static android.content.Context.MODE_PRIVATE;

import android.app.Service;
import android.content.SharedPreferences;

import androidx.fragment.app.FragmentActivity;

import com.example.fengyungo.constant.DriverConstant;
import com.example.fengyungo.constant.ResultConstant;
import com.example.fengyungo.domain.Driver;

public class SharedPreferencesUtils {

    /**
     * 从SharedPreferences中去除司机信息并封装到Driver对象中
     * @param preferences SharedPreferences
     * @return Driver
     */
    private static Driver parseDriver(SharedPreferences preferences) {
        String id = preferences.getString(DriverConstant.ID, "");
        String avatar = preferences.getString(DriverConstant.AVATAR, "");
        String nickname = preferences.getString(DriverConstant.NICKNAME, "未设置");
        String username = preferences.getString(DriverConstant.USERNAME, "123456");
        String sex = preferences.getString(DriverConstant.SEX, "未设置");
        String phone = preferences.getString(DriverConstant.PHONE, "未设置");
        String mail = preferences.getString(DriverConstant.MAIL, "未设置");
        String password = preferences.getString(DriverConstant.PASSWORD, "");
        Integer status = preferences.getInt(DriverConstant.STATUS, 0);
        String createTime = preferences.getString(DriverConstant.CREATE_TIME, "");
        String idCard = preferences.getString(DriverConstant.ID_CARD, "");

        Driver driver = new Driver();
        driver.setId(id);
        driver.setNickname(nickname);
        driver.setUsername(username);
        driver.setSex(sex);
        driver.setPhone(phone);
        driver.setPassword(password);
        driver.setIdCard(idCard);
        driver.setAvatar(avatar);
        driver.setStatus(status);
        driver.setCreateTime(createTime);
        driver.setMail(mail);

        return driver;
    }

    /**
     * 在FragmentActivity获取当前用户的信息
     *
     * @return Driver
     */
    public static Driver getDriverInfo(FragmentActivity fragmentActivity) {
        // 从SharedPreferences中获取信息
        SharedPreferences preferences = fragmentActivity.getSharedPreferences(DriverConstant.SP_NAME, MODE_PRIVATE);

        Driver driver = parseDriver(preferences);

        return driver;
    }

    /**
     * 在服务中获取当前司机的信息
     * @param service Service
     * @return Driver
     */
    public static Driver getDriverInfoInService(Service service) {
        // 从SharedPreferences中获取信息
        SharedPreferences preferences = service.getSharedPreferences(ResultConstant.DATA, MODE_PRIVATE);

        Driver driver = parseDriver(preferences);

        return driver;
    }

    /**
     * 将司机信息存入SharedPreferences中，boolean，
     * editor.putBoolean(key,value);
     * @param fragmentActivity FragmentActivity
     * @param key String
     * @param value boolean
     */
    public static void setDriverInfoToSharedPreferences(FragmentActivity fragmentActivity, String key, boolean value) {
        SharedPreferences.Editor editor = fragmentActivity.getSharedPreferences(DriverConstant.SP_NAME, MODE_PRIVATE).edit();
        // 将信息存入SharedPreferences
        editor.putBoolean(key,value);

        editor.apply();
    }

    /**
     * 将司机信息存入SharedPreferences中，String，
     * editor.putString(key,value);
     * @param fragmentActivity FragmentActivity
     * @param key String
     * @param value String
     */
    public static void setDriverInfoToSharedPreferences(FragmentActivity fragmentActivity, String key, String value) {
        SharedPreferences.Editor editor = fragmentActivity.getSharedPreferences(DriverConstant.SP_NAME, MODE_PRIVATE).edit();
        // 将信息存入SharedPreferences
        editor.putString(key,value);

        editor.apply();
    }

    /**
     * 将司机信息存入SharedPreferences中，int，
     * editor.putInt(key,value);
     * @param fragmentActivity FragmentActivity
     * @param key String
     * @param value int
     */
    public static void setDriverInfoToSharedPreferences(FragmentActivity fragmentActivity, String key, int value) {
        SharedPreferences.Editor editor = fragmentActivity.getSharedPreferences(DriverConstant.SP_NAME, MODE_PRIVATE).edit();
        // 将信息存入SharedPreferences
        editor.putInt(key,value);

        editor.apply();
    }

}
